package akinator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class GUI extends JFrame {

    private BandAkinator instance;
    private JFrame frame;
    private JLabel clock;
    private JLabel questionBody;
    private JLabel line1, line2, line3, line4;
    private int questionCount = 0;
    private JButton yesButton, noButton;
    private int decision = -1;
    private boolean canChoose = false;
    private boolean choosing = false;
    private int operation;
    private boolean hasStarted = false;
    private int random;
    private GUI ui;

    public boolean isCanChoose() {
        return canChoose;
    }

    public void setCanChoose(boolean canChoose) {
        this.canChoose = canChoose;
    }

    public void initialize(GUI gui) {
        this.ui = gui;
        //cria a interface do usuário
        gui.createUserInteface();
    }

    public void updateCount() {
        //incrementa o contador
        this.questionCount++;
        //atualiza o texto exibido
        this.line1.setText("  No " + (questionCount + 1));
    }

    //Cria a interface gráfica do sistema
    public void createUserInteface() {
        //Cria a janela principal do sistema
        frame = new JFrame("Akinator");
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setIconImage(new ImageIcon("show.gif").getImage());

        //Cria o Panel principal, com um background
        CustomPanel wrapperPanel = new CustomPanel("background.png");
        wrapperPanel.setLayout(new GridLayout(5, 1));

        //Cria um Jlabel que indica onde estão os botões
        JLabel actionsLabel = new JLabel(new ImageIcon("actionbutton.png"));
        CustomPanel customPanel = new CustomPanel("bg.png");
        customPanel.setLayout(new GridLayout(10, 1, 0, 15));
        customPanel.setPreferredSize(new Dimension(200, 100));

        //Cria um botão para iniciar as perguntas
        JButton startButton = new JButton("Começar a aplicação.");
        startButton.addActionListener(new ActionListener() {
            //inicia as perguntas
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!hasStarted) {
                    hasStarted = true;
//                    instance.routine();
                    instance = new BandAkinator(ui);
                    instance.start();

                }
            }
        });
        startButton.setIcon(new ImageIcon("add.png"));
        startButton.setSize(100, 100);
        startButton.setPreferredSize(new Dimension(230, 80));

        //Cria um botão para encerrar a execução do sistema
        JButton exitButton = new JButton("Sair da aplicação.");
        exitButton.addActionListener(new ActionListener() {
            //encerra a execução do sistema
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        exitButton.setIcon(new ImageIcon("exit.png"));
        exitButton.setSize(100, 100);
        exitButton.setPreferredSize(new Dimension(230, 80));

        customPanel.add(new JLabel());
        customPanel.add(actionsLabel);
        customPanel.add(new JLabel());
        customPanel.add(startButton);
        customPanel.add(new JLabel());
        customPanel.add(exitButton);

        //Cria um Jlabel indicando onde está a pergunta
        CustomPanel dialogPanel = new CustomPanel("");
        JLabel questionButton = new JLabel(new ImageIcon("question.png"));
        dialogPanel.add(questionButton);
        wrapperPanel.setLayout(new FlowLayout());
        wrapperPanel.add(dialogPanel);

        //Cria o Jlabel onde será exibido o relógio
        clock = new JLabel();
        //Cria o background do jlabel do relógio
        CustomPanel southPanel = new CustomPanel("button1.png");
        clock.setForeground(Color.WHITE);
        southPanel.add(clock, LEFT_ALIGNMENT);
        frame.add(southPanel, BorderLayout.SOUTH);
        ClockThread c = new ClockThread(clock);
        c.start();

        Font font = new Font("serif", Font.BOLD, 22);
        //Cria o panel onde será exibida a pergunta
        CustomPanel panel = new CustomPanel("questionDialog.png");
        panel.setPreferredSize(new Dimension(420, 250));

        //Cria os Jlabels onde estará o texto da pergunta
        line1 = new JLabel("  No " + (questionCount + 1));
        line1.setFont(font);
        line1.setForeground(Color.white);
        line2 = new JLabel("");
        line2.setFont(font);
        line2.setForeground(Color.white);
        line3 = new JLabel("");
        line3.setFont(font);
        line3.setForeground(Color.white);

        //Adiciona ao panel, o jlabels onde serão exibidas as perguntas
        panel.setLayout(new GridLayout(4, 1));
        panel.add(line1, BorderLayout.CENTER);
        panel.add(line2, BorderLayout.CENTER);
        panel.add(line3, BorderLayout.CENTER);

        Color color = new Color(Color.TRANSLUCENT);
        //Cria os botões de resposta (Sim e Não)
        yesButton = new JButton("");
        noButton = new JButton("");
        yesButton.setBackground(color);
        yesButton.setIcon(new ImageIcon("yes.png"));
        noButton.setBackground(color);
        noButton.setIcon(new ImageIcon("no.png"));

        yesButton.addActionListener(new ActionListener() {
            //Responde a pergunta com sim
            @Override
            public void actionPerformed(ActionEvent e) {
                if (canChoose) {
                    decision = 0;
                    setCanChoose(false);
                }
            }
        });

        noButton.addActionListener(new ActionListener() {
            //Responde a pergunta com não
            @Override
            public void actionPerformed(ActionEvent e) {
                if (canChoose) {
                    decision = 1;
                    setCanChoose(false);

                }
            }
        });
        //Adiciona os botões de resposta ao panel
        panel.add(yesButton);
        panel.add(noButton);

        wrapperPanel.add(panel);
        wrapperPanel.add(yesButton);
        wrapperPanel.add(noButton);
        //Adiciona à janela
        frame.add(wrapperPanel);
        frame.add(customPanel, BorderLayout.WEST);
        //frame.add(menuBar, BorderLayout.NORTH);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public JLabel getLine1() {
        return line1;
    }

    public void setLine1(JLabel line1) {
        this.line1 = line1;
    }

    public JLabel getLine2() {
        return line2;
    }

    public void setLine2(JLabel line2) {
        this.line2 = line2;
    }

    public JLabel getLine3() {
        return line3;
    }

    public void setLine3(JLabel line3) {
        this.line3 = line3;
    }

    public JLabel getLine4() {
        return line4;
    }

    public void setLine4(JLabel line4) {
        this.line4 = line4;
    }

    public int getDecision() {
        return decision;
    }

    public void setDecision(int decision) {
        this.decision = decision;
    }

    //Um Jpanel com um background
    private class CustomPanel extends JPanel {
        //Background

        private ImageIcon imageIcon;
        //Recebe o caminho da imagem de background

        public CustomPanel(String file) {
            imageIcon = new ImageIcon(file);
        }
        //Desenha o background

        @Override
        public void paintComponent(Graphics g) {
            g.drawImage(imageIcon.getImage(), 0, 0, getWidth(), getHeight(), this);
        }
    }
}
